package com.zsck.zsgy.util

import android.app.ActivityManager
import android.content.Context
import android.net.ConnectivityManager
import android.os.Handler
import android.os.Looper
import androidx.core.app.ActivityCompat
import androidx.core.content.ContextCompat
import com.blankj.utilcode.util.ToastUtils
import com.zsck.commonbase.BaseApp
import com.zsck.commonbase.R
import io.reactivex.Observable
import io.reactivex.Observer
import io.reactivex.android.schedulers.AndroidSchedulers
import io.reactivex.schedulers.Schedulers
import kotlin.random.Random

/*工具类*/

fun showToast(content: String) {
    ToastUtils.showShort(content)
}


fun Int.getDrawable() = ActivityCompat.getDrawable(BaseApp.getContext(), this)

fun Int.getResString() = BaseApp.getContext().getString(this)

fun <T> Observable<T>.subIoObsMain(observer: Observer<T>) {
    this
        .subscribeOn(Schedulers.io())
        .observeOn(AndroidSchedulers.mainThread())
        .subscribe(observer)
}

fun Int.delay(runnable: Runnable) {
    Handler(Looper.getMainLooper()).postDelayed(runnable, this.toLong())
}

fun Int.getResDimen() = BaseApp.getContext().resources.getDimension(this)

fun Int.getResDrawable() = ContextCompat.getDrawable(BaseApp.getContext(), this)

fun Int.getResColor() = ContextCompat.getColor(BaseApp.getContext(), this)

fun Any?.isNull() = this == null

fun Any?.isNotNull() = !isNull()

fun Boolean?.truely() = this != null && this

fun Boolean?.falsely() = !truely()

fun randomInt(size: Int) = Random.nextInt(0, size)


private val mColors = arrayOf(
    R.color.colorBlue.getResColor(),
    R.color.colorBlueLight.getResColor(),
    R.color.colorBlueLighter.getResColor(),
    R.color.colorBlueDark.getResColor(),
    R.color.colorCyan.getResColor(),
    R.color.colorRed.getResColor(),
    R.color.colorRedDark.getResColor(),
    R.color.colorPink.getResColor(),
    R.color.colorPurple.getResColor(),
    R.color.colorPurpleDark.getResColor(),
    R.color.colorOrigin.getResColor(),
    R.color.colorOriginDark.getResColor(),
    R.color.colorYellowDark.getResColor(),
    R.color.colorYellowDarker.getResColor(),
    R.color.colorGreen.getResColor(),
    R.color.colorGreenLight.getResColor(),
    R.color.colorGreenDark.getResColor(),
    R.color.colorGreenDarker.getResColor(),
    R.color.colorBrown.getResColor(),
    R.color.colorBrownLight.getResColor()
)

fun getRandomColor() = mColors[randomInt(mColors.size)]

fun isNetWorkAvailable(context: Context): Boolean {
    val connectivityManager =
        context.getSystemService(Context.CONNECTIVITY_SERVICE) as ConnectivityManager
    val networkInfo = connectivityManager.activeNetworkInfo
    return networkInfo != null && networkInfo.isConnected
}


